package com.laycoding.layrpc.listen;

import com.laycoding.layrpc.annotation.LayRpc;
import com.laycoding.layrpc.annotation.LayRpcService;
import com.laycoding.layrpc.common.LayRpcAnnotationCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;


import java.util.Map;
import java.util.Set;

@Slf4j
public class LayRpcListener implements ApplicationListener<ContextRefreshedEvent>{


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //拿到spring容器
        ApplicationContext applicationContext = event.getApplicationContext();

        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(LayRpc.class);

        Set<Map.Entry<String, Object>> entries = beansWithAnnotation.entrySet();


        Set<String> strings = beansWithAnnotation.keySet();
        for (String string : strings) {
            Object bean = applicationContext.getBean(string);
            LayRpcAnnotationCache.put(bean.getClass().getName(),bean.getClass());
        }
    }
}
